package com.qf.agro.web.controller.sys;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.qf.agro.common.sys.ResultInfo;
import com.qf.agro.pojo.sys.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("sys/role")
public class SysRoleController {

    @Autowired
    private com.qf.agro.service.sys.SysRoleService SysRoleService;

 



    @RequestMapping("/page")
    public ResultInfo findPage(@RequestParam(value = "page",defaultValue = "1")Integer pageNum,
                               @RequestParam(defaultValue = "3")Integer pageSize,
                               String name){
        Page page = SysRoleService.findPage( pageNum,pageSize, name);
        //  System.out.println("page===>"+page.toString());
        return ResultInfo.success(page);
    }
    @PostMapping
    public ResultInfo addSysRole(@RequestBody SysRole SysRole){
        SysRoleService.addSysRole(SysRole);
        return ResultInfo.success("添加成功");
    }

/*
    @DeleteMapping()
    public ResultInfo deleteSysRole( Long id){
        //   System.out.println(id);
        SysRoleService.deleteSysRole(id);
        return ResultInfo.success("删除成功");
    }
*/


    //editSysRole

    @PutMapping
    public ResultInfo editSysRole(@RequestBody SysRole SysRole){

        SysRoleService.editSysRole(SysRole);
        return ResultInfo.success("修改成功");
    }

    /**
     * 查询所有角色列表
     * @return
     */
    @GetMapping("/list")
    public ResultInfo findRoleList(){
        //1.调用业务层查询
        List<SysRole> roles = SysRoleService.findAll();
        //2.返回
        return ResultInfo.success(roles);
    }

    /**
     * 根据用户id，查询用户具备的角色
     * @param userId
     * @return
     */
    @GetMapping("/userRole")
    public ResultInfo findUserRole(Long userId){
        //1.调用业务层查询
        List<Long> roleIds = SysRoleService.findUserRole(userId);
        //2.返回
        return ResultInfo.success(roleIds);
    }

    @PostMapping("/userRole")
    public ResultInfo updateUserRole(@RequestBody Map<String,Object>map){
        Long userId = Long.parseLong((String) map.get("userId"));
        List<Long> roleIds = (List<Long>)map.get("roleIds");
        SysRoleService.updateUserRole(userId,roleIds);
        return ResultInfo.success(null);
    }

    @DeleteMapping
    public ResultInfo delete(Long id){
        //1.调用业务层删除
        SysRoleService.delete(id);
        //2.返回
        return ResultInfo.success(null);
    }
}

